var aes = require('./aes.js'); //引用AES源码js
//十六位十六进制数作为秘钥

var aeskey = aes.CryptoJS.enc.Utf8.parse('zxcvbnmasdfghjk2'); //十六位十六进制数作为秘钥偏移量

var aesiv = aes.CryptoJS.enc.Utf8.parse('mnbvcxzlkjhgfda1'); // 加密

function encrypt(data) {
    var srcs = aes.CryptoJS.enc.Utf8.parse(data);
    var encrypted = aes.CryptoJS.AES.encrypt(srcs, aeskey, {
        iv: aesiv,
        mode: aes.CryptoJS.mode.CBC,
        padding: aes.CryptoJS.pad.Pkcs7
    }); //返回base64加密结果

    return encrypted.toString();
} //解密

function decrypt(data) {
    // data是base64编码数据
    var decrypt = aes.CryptoJS.AES.decrypt(data, aeskey, {
        iv: aesiv,
        mode: aes.CryptoJS.mode.CBC,
        padding: aes.CryptoJS.pad.Pkcs7
    });
    var decryptedStr = decrypt.toString(aes.CryptoJS.enc.Utf8);
    return decryptedStr.toString();
} //暴露接口

module.exports = {
    encrypt: encrypt,
    decrypt: decrypt
};
